package com.carworld.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.carworld.config.BaseController;
import com.carworld.model.BimSpares;

public class ExcelController extends BaseController {

	public String excelPath = "C://Users/25532/Desktop/lqf_service.xls";

	public void poimothed() throws FileNotFoundException {
		try {
			// String encoding = "GBK";
			File excel = new File(excelPath);
			if (excel.isFile() && excel.exists()) { // 判断文件是否存在
				String[] split = excel.getName().split("\\."); // .是特殊字符，需要转义！！！！！
				Workbook wb = null;
				// 根据文件后缀（xls/xlsx）进行判断
				if ("xls".equals(split[1])) {
					FileInputStream fis = new FileInputStream(excel); // 文件流对象
					wb = new HSSFWorkbook(fis);
				} else if ("xlsx".equals(split[1])) {
					//
				} else {
					System.out.println("文件类型错误!");
					return;
				}
				// 开始解析
				Sheet sheet = wb.getSheetAt(0); // 读取sheet 0
				int firstRowIndex = sheet.getFirstRowNum() + 1; // 第一行是列名，所以不读
				int lastRowIndex = sheet.getLastRowNum();
				System.out.println("firstRowIndex: " + firstRowIndex);
				System.out.println("lastRowIndex: " + lastRowIndex);

				for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { // 遍历行
					System.out.println("rIndex: " + rIndex);
					Row row = sheet.getRow(rIndex);
					if (row != null) {
						int firstCellIndex = row.getFirstCellNum();
						System.out.println("firstCellIndex：" + firstCellIndex);
						int lastCellIndex = row.getLastCellNum();
						System.out.println("lastCellIndex：" + lastCellIndex);

						String spareName = null; // 商品名
						String spareKind = null; // 商品类型
						String sparePrice = null; // 单价

						for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) { // 遍历列
							Cell cell = row.getCell(cIndex);
							if (cell != null && !"".equals(cell.toString().trim())) {
								switch (cIndex) {
								case 0:
									spareName = cell.toString();
									System.out.println("0:" + spareName);
									break;
								case 1:
									spareKind = cell.toString();
									System.out.println("2:" + spareKind);
									break;
								case 2:
									sparePrice = cell.toString();
									System.out.println("6:" + sparePrice);
									break;
								default:
									break;
								}
							}
						}

						Date date = new Date();
						// 存入 Bim_spares表中
						BimSpares bimSpares = new BimSpares();
						bimSpares.setSpareId(date.getTime());
						bimSpares.setSpareName(spareName);
						bimSpares.setSpareKind(spareKind);
						bimSpares.setSpareStock(1000);
						bimSpares.setSparePrice(new BigDecimal(sparePrice));
						bimSpares.setSpareCreatetime(date);
						bimSpares.setSpareUpdatetime(date);
						bimSpares.setSpareCatalog("0");
						bimSpares.setSpareUserfor("4640bb59e5bd46d893a73ce6989c8859");
						bimSpares.setSpareFactory("上海南汇车仕界共享店铺形象店");
						bimSpares.save();
					}
				}

			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
